import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

import com.mysql.cj.protocol.Resultset;

public  class JDBCUtils{
    public static Connection getConnection(){
        InputStream ip=null;
        Connection conn=null;
        try {
            ip=ClassLoader.getSystemClassLoader().getResourceAsStream("info.properties");
            Properties pros=new Properties();
            pros.load(ip);
            String user=pros.getProperty("user");
            String password=pros.getProperty("password");
            String url=pros.getProperty("url");
            String driverClass=pros.getProperty("driverClass");
            Class.forName(driverClass);
            conn=DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
            //TODO: handle exception
        }finally{
            try {
                if(ip!=null){
                    ip.close();
                }
            } catch (Exception e) {
                //TODO: handle exception
            }
        }
        return conn;
    }
    public static void closeResource(Connection conn,PreparedStatement ps){
        try {
            if(ps!=null){
                ps.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            //TODO: handle exception
        }
        try {
            if(conn!=null){
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            //TODO: handle exception
        }
    }


    public static void closeResource(Connection conn,PreparedStatement ps,ResultSet rs){
        try {
            if(ps!=null){
                ps.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            //TODO: handle exception
        }
        try {
            if(conn!=null){
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            //TODO: handle exception
        }
        try {
            if(rs!=null){
                rs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            //TODO: handle exception
        }
    }
}